package com.a3dweijing.wetouch2.base;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.tools.SharedPreUtil;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import okhttp3.Call;

/**
 * Created by Yxd on 2017/10/11.
 */

public class BaseFragmentActivity extends FragmentActivity
{
    private PopupWindow popupWindowforrequest;

    public ArrayList<retrofit2.Call> calls=new ArrayList<>();

    public ArrayList<Disposable> disposables=new ArrayList<>();
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 强制app方向
        // 沉浸式状态栏 暂未使用
        // if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Window window = getWindow();
        // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
        // );
        // window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        // | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        // window.setStatusBarColor(Color.TRANSPARENT);
        // }
        Boolean iswhite = SharedPreUtil.getBoolean(getBaseContext(), "iswhite", false);
        if (iswhite)
        {
            
        }
        super.onCreate(savedInstanceState);
    }
    
    /**
     * 显示加载用的popupwindow
     *
     * @param context 当前上下文
     * @param s popupwindow 显示的文字
     */
    public void startPopupwindow(Context context, String s)
    {
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_request, null);
        TextView textView = (TextView)view.findViewById(R.id.popwind_get_ipcam_search_txt);
        textView.setText(s);
        popupWindowforrequest =
            new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindowforrequest.setBackgroundDrawable(new ColorDrawable(0x0000000));
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.2f;
        getWindow().setAttributes(lp);
        popupWindowforrequest.setOnDismissListener(new PopupWindow.OnDismissListener()
        {
            @Override
            public void onDismiss()
            {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
        popupWindowforrequest.setFocusable(true);
        popupWindowforrequest.showAtLocation(view, Gravity.CENTER, 0, 0);
        popupWindowforrequest.setTouchInterceptor(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent)
            {
                return false;
            }
        });
    }
    
    /**
     * 关闭当前的popupwindow
     */
    public void endPopupwindow()
    {
        if (popupWindowforrequest != null && popupWindowforrequest.isShowing())
        {
            popupWindowforrequest.dismiss();
        }
    }
    
    @Override
    protected void onDestroy()
    {
        for (retrofit2.Call call : calls) {
            if (call != null && !call.isCanceled())
                call.cancel();
        }
        for (Disposable disposable : disposables) {
            if (disposable != null && !disposable.isDisposed())
            {
                disposable.dispose();
            }
        }
        super.onDestroy();
    }
}
